//
//  ZZCTRUbiController.m
//  ZZC-Finance
//
//  Created by mac on 2017/5/24.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ZZCTRUbiController.h"
#import "ZZCTRUbiCell.h"
#import "ZZCTRUbi.h"
#import "ZZCNullRecordView.h"

static NSString *TRUbiCell = @"TRUbiCell";
@interface ZZCTRUbiController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) UITableView *TRUbiTableView;
@property (strong, nonatomic) ZZCNullRecordView *nullView;
@property (strong, nonatomic) NSMutableArray *trUbiData;


@end

@implementation ZZCTRUbiController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = ZZCBGColor;
    [self requestDueData];
    [self.view addSubview:self.TRUbiTableView];
    
}
- (void)requestDueData {
    NSString *token = ZZCNSUserDefaults(@"token");
    NSDictionary *dict = @{@"token":token,@"page":@(1)};
    NSString *sign = [ZZCRequest getSignWithDict:dict];
    sign = [NSString md5:[sign lowercaseString]];
    NSDictionary *param = @{@"timestamp":[NSString timeStr],@"sign":sign,@"token":token,@"page":@(1)};
    [[ZZCRequest sharedNetworkTools] POST:ZZCURL_API_URECORD parameters:param finished:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            ZZCLog(@"失败-%@",error);
        }else {
            ZZCLog(@"-**%@",responseObject);
//            self.count = responseObject[@"data"][@"count"];
//            self.dueModel = [ZZCDue mj_objectWithKeyValues:responseObject[@"data"][@"list"]];
//            [self.dueTableView reloadData];
        self.trUbiData = [ZZCTRUbi mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"list"]];
            NSLog(@"$$$$$$$$$$$$$$$$$$$$%@",_trUbiData);
            [self.TRUbiTableView reloadData];
        }
        if ([responseObject[@"status"] isEqual:@(0)]) {
            [self.view addSubview:_nullView];
            NSLog(@"$#########$#$#$#$#$#$#$#$#$#$+");
        }
    }];
}


#pragma mark - 数据源方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    return 10;
    return self.trUbiData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZZCTRUbiCell *cell = [tableView dequeueReusableCellWithIdentifier:TRUbiCell];
    cell.selectionStyle = 0;
    
//    cell.beLeftLabel.text = [NSString stringWithFormat:@"剩余：%.02fU币",(float)indexPath.row*100];
    //给模型传值过去
    cell.TrubiModel = self.trUbiData[indexPath.row];
    NSLog(@"###########$$$$$$$%@",cell.TrubiModel);
//    cell.refundBtn.tag = indexPath.row;
//    [cell.refundBtn addTarget:self action:@selector(refundBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    //添加长按手势
    UILongPressGestureRecognizer * longPressGesture =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(cellLongPress:)];
    
    longPressGesture.minimumPressDuration=1.5f;//设置长按 时间
    [cell addGestureRecognizer:longPressGesture];

    return cell;
}

//长按手势响应编辑模式
- (void)cellLongPress:(UILongPressGestureRecognizer *)longRecognizer {
    [self.TRUbiTableView setEditing:YES animated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 90;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    //    [tableView setEditing:YES animated:YES];
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView setEditing:NO animated:YES];
    ZZCLog(@"删除了第%ld",(long)indexPath.row);
    [self deleteDataAction];
    switch (editingStyle) {
        case UITableViewCellEditingStyleDelete:
        {
            //修改数据源，在刷新 tableView
            [_trUbiData removeObjectAtIndex:indexPath.row];
            
            //让表视图删除对应的行
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView setEditing:NO animated:YES];
        }
            break;
            //        case UITableViewCellEditingStyleInsert:
            //        {
            //            [_dataSource insertObject:@"我是新增" atIndex:indexPath.row];
            //            //让表视图添加对应的行
            //            [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            //        }
            //            break;
            
        default:
            break;
    }

}
- (void)deleteDataAction {
    NSString *token = ZZCNSUserDefaults(@"token");
    NSDictionary *dict = @{@"token":token,@"id":@"0"};
    NSString *sign = [ZZCRequest getSignWithDict:dict];
    sign = [NSString md5:[sign lowercaseString]];
    NSDictionary *param = @{@"timestamp":[NSString timeStr],@"sign":sign,@"token":token,@"id":@"0"};
    [[ZZCRequest sharedNetworkTools] POST:ZZCURL_API_CREDITRECORDDELETE parameters:param finished:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            ZZCLog(@"失败-%@",error);
        }else {
            ZZCLog(@"-**00000%@",responseObject);
        }
    }];
}
#pragma mark - 懒加载
- (UITableView *)TRUbiTableView {
    NSLog(@"%@",_trUbiData);
    if (_TRUbiTableView == nil) {
        _TRUbiTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ZZCScreenW, ZZCScreenH-108)];
        [_TRUbiTableView registerNib:[UINib nibWithNibName:@"ZZCTRUbiCell" bundle:nil] forCellReuseIdentifier:TRUbiCell];
        _TRUbiTableView.delegate = self;
        _TRUbiTableView.dataSource = self;
        _TRUbiTableView.separatorStyle = NO;
        _TRUbiTableView.showsVerticalScrollIndicator = NO;
        _TRUbiTableView.backgroundColor = ZZCBGColor;
        
    }
    return _TRUbiTableView;
}
- (ZZCNullRecordView *)nullView {
    if (_nullView == nil) {
        _nullView = [[ZZCNullRecordView alloc] initWithFrame:CGRectMake(0, 0, 300, 600)];
    }
    return _nullView;
}
- (NSMutableArray *)trUbiData {
    if (_trUbiData == nil) {
        _trUbiData = [NSMutableArray array];
    }
    return _trUbiData;
}

@end
